package com.example.remindme.RemindModule.DataCard.Fragment

import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.remindme.RemindModule.DataCard.DataBase.MatterDatabase
import com.example.remindme.RemindDatabase.MatterEntity
import com.example.remindme.RemindModule.DataCard.ViewHolder.ClickData
import kotlinx.coroutines.coroutineScope

val SEARCHANS="searchANS"
val QUERY_CODE_ALL=101
val QUERY_CODE_TODAY=102
class TaskViewModel(application: android.app.Application) : AndroidViewModel(application) {

    var finishRoom=MutableLiveData<Int>()
    // 当前进入次taskfragment的
     var listType=MutableLiveData<String>()

    // 记录根据 list选择的数据
    var liveDataTask:LiveData<List<MatterEntity>>? = null
    // 记录以下根据block选择的数据
    var AllDataTask:LiveData<List<MatterEntity>>? = null
    var FlagDataTask:LiveData<List<MatterEntity>>? = null
    var ScheduledDataTask:LiveData<List<MatterEntity>>? = null

   var current: ClickData?=null

    val matterDatabase by lazy {
        MatterDatabase.getDatabase(getApplication())  // 数据库在实例化的时候需要。。Context
    }
    val matterDao by lazy {
        matterDatabase.matterDao()
    }

    suspend fun queryRoom() {
        coroutineScope {
           AllDataTask = matterDao.getAll()
            finishRoom.postValue(QUERY_CODE_ALL)
        }
    }
    fun getData(): LiveData<List<MatterEntity>>? {
        return liveDataTask
    }

    fun getAllData(): LiveData<List<MatterEntity>>? {
        return AllDataTask
    }
    fun getTodayData(): LiveData<List<MatterEntity>>? {
        return ScheduledDataTask
    }
    fun getFlagData(): LiveData<List<MatterEntity>>? {
        return FlagDataTask
    }
    fun getScheduleData(): LiveData<List<MatterEntity>>? {
        return ScheduledDataTask
    }
    // 只需定义一个出口，外面调用不同的数据库查询函数 就可以


    suspend fun queryRoomByType(Tag:String) {
        coroutineScope {
           liveDataTask= matterDao.getByType(Tag)
            finishRoom.postValue(0)
        }
    }
    suspend fun deleteFromRoom(entity: MatterEntity) {
        coroutineScope {
           matterDao.deleteAccount(entity)
        }
    }
    suspend fun InsertRoom(entity: MatterEntity) {
        coroutineScope {
           matterDao.insertRegister(entity)
            finishRoom.postValue(0)
        }
    }
    suspend fun queryRoomFlagged() {
        coroutineScope {
            FlagDataTask= matterDao.getMattersFlaged()
            finishRoom.postValue(0)
        }
    }
    suspend fun queryRoomByTimed() {
        coroutineScope {
            ScheduledDataTask= matterDao.getMatterTimed()
            finishRoom.postValue(0)
        }
    }
    suspend fun queryRoomToday() {
        coroutineScope {
            liveDataTask= matterDao.getMatterTimed()
            finishRoom.postValue(0)
        }
    }

}